package com.ruoyi.common.utils.utils.json;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class JsonUtils {
    private static final ObjectMapper mapper = new ObjectMapper();

    public static Object getJsonValue(String jsonStr, String... keys) throws IOException {
        JsonNode node = mapper.readTree(jsonStr);
        for (String key : keys) {
            if (node.isObject() && node.has(key)) {
                node = node.get(key);
            } else {
                return null;
            }
        }
        return node.isValueNode() ? node.asText() : node;
    }

    /**
     * 处理{}这种类型的数据
     *
     * @param json
     * @return
     */
    public static String parseMiddleData(String json, String parse) {
        JSONObject jsonObject = JSONUtil.parseObj(json);
        return jsonObject.getStr(parse);
    }

}
